/**
 * 来自angular或@ionit/angular
 */
import { Component,OnInit, ViewChild } from '@angular/core';
import { IonRouterOutlet,Platform,MenuController,NavController} from '@ionic/angular';
import {Router,NavigationStart} from '@angular/router';
/**
 * 插件市场
 */
import { SplashScreen } from '@awesome-cordova-plugins/splash-screen/ngx';
import { StatusBar } from '@awesome-cordova-plugins/status-bar/ngx';
import { CodePush } from '@awesome-cordova-plugins/code-push/ngx';
/**
 * 当前项目自定义
 */
import { BackButtonService } from './services/back-button.service';
import { NativeService } from './services/native.service';
import { PushService } from './services/push.service';
import { ChatService } from './services/chat.service';
import { TdTouchService } from './services/td-touch.service';

@Component({
	selector: 'app-root',
	templateUrl: 'app.component.html'
})
export class AppComponent implements OnInit {

	public static tabsIonRouterOutlet: IonRouterOutlet;
	
	/**
	 * @View依据@Detrive指令类型装载组件实例，IonRouterOutlet用法基本同RouterOutlet但没有找到文档支持这一点，
	 * 		我从源码中看到他就是自己写的一个@Detrive而不是继承自RouterOutlet。
	 */
	@ViewChild(IonRouterOutlet,{static:true})
	public rootIonRouterOutlet: IonRouterOutlet;

    public currentUrl = ''; 
    public showMenus = false;

	constructor(
		private platform:Platform,
		private menu:MenuController,
		private nav:NavController,
		private router:Router,
		private splashScreen:SplashScreen,
		private statusBar:StatusBar,
		private backButtonService:BackButtonService,
		private nativeService:NativeService,
		private push:PushService,
		private chat:ChatService,
		private codePush:CodePush,
		private tdtouch:TdTouchService		
	) {
		//能否写到igOninit中？
		this.initializeApp();
	}
	
	/**
	 * @Desc 初始化
	 */
	ngOnInit(): void {
		
	}

	/**
	 * @desc 激活路由，如果当前路由有onPageDidEnter方法则调用。
	 * @param evt 
	 */
	 onActivate(evt){		
		if(evt.onPageDidEnter) evt.onPageDidEnter();
		this.showMenus=false;
	}

	/**
	 * @Desc 离开路由
	 * @param evt 
	 */
	onDeactivate(evt){
		if(evt.onPageDidLeave) evt.onPageDidLeave;
	}	

	private async initStatusBar() {
        this.statusBar.overlaysWebView(true);
        this.statusBar.styleDefault();
    }

	/**
	 * 这个业务没有完全写好，我没有极光推送的账号，没法安装插件，只提供一个方法在这里
	 */
	private async initializeJiGuang() {
        if (this.nativeService.isMobile()) {
            await this.push.init();            
        }
    }

	private async onPlatformResumed() {
        // this.platform.resume.subscribe(() => {
        //     console.log('App Resumed1');
        //     this.push.reduceBadgeNumber();
        //     this.chat.resetBadge();
        //     setTimeout(() => {
        //         if (AppComponent.tabsOutlet && AppComponent.tabsOutlet.isActivated) {
        //             console.log('APP resume and page now is tabs');
        //             const component: any = AppComponent.tabsOutlet.component;
        //             if (component.onPageDidEnter) {
        //                 component.onPageDidEnter();
        //             }
        //         } else {
        //             const component: any = this.rootOutlet.component;
        //             if (component.onPageDidEnter) {
        //                 component.onPageDidEnter();
        //             }
        //         }
        //     }, 500);
        // });
    }
	initializeApp(){
		this.platform.ready().then(async ()=>{
			/**
			 * CodePush相关信息配置，CodePush可能要完整用起来
			 */
			await this.codePush.notifyApplicationReady().then((res)=>{
				console.log(res);	//通知成功，输出OK				
			}).catch((err)=>{
				console.log(err);
			}).finally(()=>{
				console.log("notifyApplicationReady finally");
			});
			await this.codePush.getCurrentPackage().then((res)=>{
				console.log(res);	//失败时updata为null,否则update为包含版本信息、包大小的json数据			
			}).catch((err)=>{				
				console.log(err);
			});
			/**
			 * 网络相关判断
			 */
			if(this.nativeService.isConnecting()){				
				this.initStatusBar();
				/**
				 * 默认back button已经被改写，行为可能与预期有差别，需要以那里的为准
				 */
				await this.backButtonService.registerBackButton(this.rootIonRouterOutlet);
				await this.initializeJiGuang();
				await  this.onPlatformResumed();

				// TdTouchService.navigateOnHomePageEntered = new EventEmitter<any>();
                // this.tdtouch.checkAvailability();
			}else{
				//在我家里的环境模拟器network.type是4g，在公司有时候返回unknown,nativeService中认为只有none是认为是无网络
				//console.log("无网络");
				await this.nativeService.showAlert('您的网络已断开，请连接网络后重新打开APP');               
			}
			/**
			 * 手动关闭splashScreen,原作者封装hideSplashScreen()方法，我觉得没有必要
			 */	
			setTimeout(() => {
				this.splashScreen.hide();
			}, 500);
		})
	}
}
